package com.joinway.admin.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.joinway.admin.bean.view.CodeHistoryView;
import com.joinway.framework.data.jdbc.repository.TableRepository;
import com.joinway.framework.extension.utils.DataUtils;
import com.joinway.platform.bean.domain.AutocodeHistory;

@Service
public class AppService {

	@Autowired TableRepository repository;
	
	public void saveSnapshot(String dom, String loginUser, int type, String table) throws Exception {
		AutocodeHistory ah = new AutocodeHistory();
		
		ah.setDom(dom.getBytes("UTF-8"));
		ah.setCodeType(DataUtils.convertToInt(type));
		ah.setTargetTable(table);
		ah.setUpdateBy(loginUser);
		ah.setUpdateDate(new Date());
		
		repository.save(ah);
	}
	
	public List<AutocodeHistory> findAutocodeHistories(String table, int type, String owner){
//		String sql = "select * from autocode_history where target_table = :table and code_type = :codeType "
//		
//		return repository.find(AutocodeHistory, sql, [table:table, codeType:type])
		return new ArrayList<>();
	}
	
	public CodeHistoryView findSnapShot(int id) throws Exception {
		AutocodeHistory history = repository.findById(AutocodeHistory.class, id);
		
		return new CodeHistoryView(new String(history.getDom(), "UTF-8"));
	}
}
